package org.gen.code;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@SpringBootApplication
public class GenCodeApplication implements ApplicationListener<WebServerInitializedEvent> {

    public static void main(String[] args) {
        SpringApplication.run(GenCodeApplication.class, args);
        System.out.println("启动成功 >>>>>>>>>>>>>>>>>>>>>>>>");
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        try {
            WebServer webServer = event.getWebServer();
            WebServerApplicationContext applicationContext = event.getApplicationContext();
            Environment environment = applicationContext.getEnvironment();
            String ip = Inet4Address.getLocalHost().getHostAddress();
            int port = webServer.getPort();
            String contextPath = environment.getProperty("server.servlet.context-path");
            if (contextPath == null) {
                contextPath = "";
            }
            log.info("\n---------------------------------------------------------\n" +
                    "\tApplication is running! Access address:\n" +
                    "\tLocal:\t\thttp://localhost:{}" +
                    "\n\tExternal:\thttp://{}:{}{}" +
                    "\n---------------------------------------------------------\n", port, ip, port, contextPath);
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }

    }
}
